package db

import "gorm.io/gorm"

// Game 赛事表
type Game struct {
	gorm.Model
	Name     string `json:"name" binding:"required" gorm:"comment:赛事名称"`
	Type     string `json:"type" binding:"required,oneof=wheel_war final" gorm:"comment:wheel_war：车轮战，final：决赛"`
	PlayerA  string `json:"playerA" binding:"required" gorm:"comment:战队A名称"`
	FileA    string `json:"fileA" binding:"required" gorm:"comment:战队A文件"`
	PlayerB  string `json:"playerB" binding:"required" gorm:"comment:战队B名称"`
	FileB    string `json:"fileB" binding:"required" gorm:"comment:战队B文件"`
	BattleId int    `json:"battleId" binding:"required" gorm:"comment:对战ID"`
	MapFile  string `json:"map" binding:"required" gorm:"comment:地图文件地址"`
	AIRank   int    `json:"aiRank" binding:"oneof=0 1" gorm:"comment:是否为AIRank模式，1：是，0：否"`
	ECount   int    `json:"eCount" binding:"gte=0" gorm:"comment:AI敌人数量"`
	Debug    int    `json:"debug" binding:"oneof=0 1" gorm:"comment:是否为debug模式，1：是，0：否"`
	GameOver int    `json:"gameOver" binding:"oneof=0 1" gorm:"comment:该场比赛是否已完赛，1：是，0：否"`
	Weather  string `json:"weather" binding:"required" gorm:"comment:天气模式"`
	Tool     string `json:"tool" binding:"required" gorm:"comment:道具模式"`
}

const GameOverY = 1

type GameRecord struct {
	gorm.Model
	GroupName string `gorm:"comment:赛事名称"`
	BattleId  int    `gorm:"comment:比赛ID"`
	Type      int    `gorm:"comment:比赛类型，1：A组AI RANK战；2：B组车轮战；3：预决赛车轮战；4：决赛"`
	AIRank    int    `gorm:"comment:是否为AIRank模式，1：是，0：否"`
	Player    string `gorm:"comment:战队名称"`
	Fire      int    `gorm:"comment:开火次数"`
	Hit       int    `gorm:"comment:击中数"`
	Kill      int    `gorm:"comment:击杀数"`
	Life      int    `gorm:"comment:剩余血量"`
	CostTime  int    `gorm:"comment:比赛耗时"`
	Win       int    `gorm:"comment:是否赢得了比赛，1：是，0：否"`
}

type WheelWarRank struct {
	Rank     int
	Player   string
	Fire     int
	Hit      int
	Kill     int
	Life     int
	CostTime int
	Rounds   int
}

func QWheelWarRanks(groupName string, typ int) (result []WheelWarRank) {
	result = []WheelWarRank{}
	// 由于本次申请的MySQL数据库开启了ONLY_FULL_GROUP_BY，导致出现this is incompatible with sql_mode=only_full_group_by报错。
	// 解决方式：1、修改数据库配置将其关闭；2、将查询的非group by字段使用函数ANY_VALUE()包含，如有疑问请自行查阅文档：https://dev.mysql.com/doc/refman/5.7/en/miscellaneous-functions.html#function_any-value。
	sql := "select ANY_VALUE(t.name) as player, ANY_VALUE(sum(rec.fire)) as fire, ANY_VALUE(sum(rec.hit)) as hit, ANY_VALUE(sum(rec.`kill`)) as `kill`, ANY_VALUE(sum(rec.life)) as life, ANY_VALUE(sum(rec.cost_time)) as cost_time, ANY_VALUE(count(*)) as rounds " +
		"FROM game_records rec, teams t where rec.group_name = ? and rec.type = ? and t.uid = rec.player group by rec.player order by `kill` desc"
	DB.Debug().Raw(sql, groupName, typ).Scan(&result)
	return result
}

type FinalRank struct {
	Rank   int
	Player string
	Win    int
	Kill   int
}

func QFinalRanks(groupName string) (result []FinalRank) {
	result = []FinalRank{}
	sql := "select ANY_VALUE(t.name) as player, ANY_VALUE(sum(rec.`kill`)) as `kill`, ANY_VALUE(sum(rec.win)) as win " +
		"FROM game_records rec, teams t where rec.group_name = ? and rec.type = 'final' and t.uid = rec.player group by rec.player order by `win` desc, `kill` desc"
	DB.Debug().Raw(sql, groupName).Scan(&result)
	return result
}

type AIRank struct {
	Rank     int
	Player   string
	Kill     int
	CostTime int
}

func QAIRanks(groupName string) (result []AIRank) {
	result = []AIRank{}
	sql := "select ANY_VALUE(t.name) as player, ANY_VALUE(sum(rec.`kill`)) as `kill`, ANY_VALUE(sum(rec.cost_time)) as `cost_time` " +
		"FROM game_records rec, teams t where rec.group_name = ? and rec.ai_rank = 1 and t.uid = rec.player group by rec.player order by `kill` desc, cost_time asc"
	DB.Debug().Raw(sql, groupName).Scan(&result)
	return result
}
